package com.carolouvindam;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.carolouvindam.Model.Product;

import java.util.ArrayList;
import java.util.List;

public class GridImageAdaptater extends BaseAdapter {

    // references to our images
    private Integer[] mThumbIds;
    private Context mContext;
    private List<Product> productArrayList;

    public GridImageAdaptater(Context c, List<Product> productArrayList) {
        mContext = c;
        this.productArrayList = productArrayList;
        this.mThumbIds = this.getPicturesId();
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    private Integer[] getPicturesId() {


        ArrayList<Integer> listPictureItem = new ArrayList<Integer>();

        for (int i = 0; i < this.productArrayList.size(); i++) {
            listPictureItem.add(this.productArrayList.get(i).getIllustration());
        }


        return this.toIntArray(listPictureItem);

    }

    private Integer[] toIntArray(List<Integer> integerList) {
        Integer[] intArray = new Integer[integerList.size()];
        for (int i = 0; i < integerList.size(); i++) {
            intArray[i] = integerList.get(i);
        }
        return intArray;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
            imageView.setTag( this.productArrayList.get(position).getIdDb());
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }
    public String positionToDescription(int position)
    {
        return this.productArrayList.get(position).getDescription();

    }

}


